package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.pab.ebank.PabOrderMessageRes;

@Controller
@Scope("prototype")
public class PabB2cFrontRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(PabB2cFrontRes.class);
	
	@RequestMapping(value = "/pabB2cFrontRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- PabB2cFrontRes bankback ---------------");		
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		PabOrderMessageRes message = new PabOrderMessageRes();
		/**
		 * 订单号
		 */
		String orderNo = request.getParameter("OrderNo");
		/**
		 * 交易金额
		 */
		String payAmount = request.getParameter("PayAmount");
		String currency = request.getParameter("Currency");
		/**
		 * 交易标志1-支付成功,2-支付失败,3-支付结果未知
		 */
		String payFlag = request.getParameter("PayFlag");
		String tranTime = request.getParameter("TranTime");
		//String errorCode = request.getParameter("ErrorCode");
		String signMessage = request.getParameter("Signature");
		message.setBank(BankConstants.BANK_LOGO_PAB);
		message.setFront(true);
		message.setCurrency(currency);
		message.setTradeDate(tranTime);
		message.setSignMessage(signMessage);
		message.setCommitFlag(payFlag);
		message.setBankOrderId(orderNo);
		message.setOrderId(orderNo);
		message.setAmount(new BigDecimal(payAmount));
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		
		Map<String, String> param = (Map<String, String>) result
				.getModel("isNotifyCashier");
		/**
		 * 订单号
		 */
		String gopayOrderID = param.get("orderId");
		/**
		 * 订单金额
		 */
		String orderAmt = param.get("amt");
		/**
		 * 支付状态
		 */
		String payStatus = param.get("payStatus");
		if ("00".equals(payStatus)) {
			orderInfo.setOrderId(gopayOrderID);
			orderInfo.setAmt(orderAmt);
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("00");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			return null;
		}
		return orderInfo;
	}

	
	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("Signature"))|| StringUtils.isEmpty(request.getParameter("OrderNo"))) {
			return false;
		}
		return true;
	}
}
